﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using DG.Tweening;
using UnityEngine.EventSystems;
using UnityEngine.Events;

namespace JJYYCore.UIComponents.Slide
{
    public class SlideUI : MonoBehaviour
    {
        [SerializeField]
        private bool activeDebug;
        [SerializeField]
        private float actionTime = 0.5f;
        [SerializeField]
        private HorizontalLayoutGroup pages;

        private int activeIndex;
        private float dirMark;

        private bool wasHit = false;

        private Vector3 startPos, activePos
        , nodeStartPos;

        private float halfScreenWidth = Screen.width / 2;

        public bool IsMouseInArea { set; get; } = true;
        public SlideAction slideAction;

        #region "MonoBehavior"
        private void Awake()
        {
            slideAction += (int value) =>
            {

            };
        }

        void Update()
        {
            if (IsMouseInArea)
                DragListener();
            else if (wasHit && Input.GetMouseButtonUp(0))
                ClearMoveListener();
        }
        #endregion

        public void MoveToIndexImmediately(int index)
        {
            StartCoroutine(WaitOneFrameToMove(() =>
            {
                MoveToIndexImmediatelyReal(index);
            }));
        }

        public void MoveToIndex(int index)
        {
            StartCoroutine(WaitOneFrameToMove(() =>
            {
                MoveToIndexReal(index);
            }));
        }

        private IEnumerator WaitOneFrameToMove(UnityAction action)
        {
            int frameCount = 0;

            while (frameCount <= 2)
            {
                frameCount += 1;
                yield return null;
            }


            for (int i = 0; i < pages.transform.childCount; i++)
            {
                var selected = pages.transform.GetChild(i);
                Debug.Log("子对象名称为:"+selected.name);
            }

            action();
        }

        private void MoveToIndexImmediatelyReal(int index)
        {
            SetActiveIndex(index);

            var origonalPos = pages.transform.position;
            origonalPos.x = GetXOffset();

            pages.transform.position = origonalPos;
        }

        private void MoveToIndexReal(int index)
        {
            if (index < 0 || index >= pages.transform.childCount)
            {
                Debug.LogError("移动到Transform的子对象失败!索引没在范围内。索引为:" + index);
                return;
            }

            SetActiveIndex(index);
            MoveToActiveIndex();
        }

        private void DragListener()
        {
            if (Input.GetMouseButtonDown(0))
            {
                wasHit = true;
                startPos = Input.mousePosition;
                nodeStartPos = pages.transform.position;
            }
            else if (Input.GetMouseButton(0))
            {
                activePos = Input.mousePosition;
                MoveDirListener();
            }
            else if (Input.GetMouseButtonUp(0))
            {
                ClearMoveListener();
            }
        }

        private void ClearMoveListener()
        {
            wasHit = false;
            MoveToListener();
            slideAction(activeIndex);
        }

        private void MoveDirListener()
        {
            dirMark = activePos.x - startPos.x;
            dirMark = Mathf.Clamp(dirMark, -halfScreenWidth, halfScreenWidth);

            var moveDir = Vector3.right * dirMark;
            pages.transform.position = nodeStartPos + moveDir;
        }

        private void MoveToListener()
        {
            if (dirMark > 0)
                MoveBack();
            else if (dirMark < 0)
                MoveForward();
        }

        private void MoveForward()
        {
            var nextIndex = Mathf.Min(activeIndex + 1, pages.transform.childCount - 1);

            if (nextIndex == activeIndex)
            {
                MoveToActiveIndex();
                return;
            }
            else
            {
                SetActiveIndex(nextIndex);
                MoveToActiveIndex();
            }
        }

        private void MoveBack()
        {
            var nextIndex = Mathf.Max(activeIndex - 1, 0);

            if (nextIndex == activeIndex)
            {
                MoveToActiveIndex();
                return;
            }
            else
            {

                SetActiveIndex(nextIndex);
                MoveToActiveIndex();
            }
        }

        private void MoveToActiveIndex()
        {
            var targetPos = GetXOffset();
            pages.transform.DOMoveX(targetPos, actionTime);
        }

        private float GetXOffset()
        {
            var targetPos = pages.transform.GetChild(activeIndex).position;
            Debug.Log("起始位置为:"+targetPos+"。并且节点名称为:"+pages.transform.GetChild(activeIndex).name);
            var startPos = targetPos;

            targetPos = pages.transform.position - targetPos;
            Debug.Log("起始位置为:"+startPos+"目标位置为:" +targetPos);
            return targetPos.x -= halfScreenWidth;
        }

        private void SetActiveIndex(int nextIndex)
        {
            activeIndex = nextIndex;
            slideAction(activeIndex);
        }
    }

    public delegate void SlideAction(int index);
}